<?
package("japhax.sql");

import("japhax.sql.Database");

/**
 * $Id: Mysql.php 9 2010-04-25 05:02:05Z bsdlite $
 *
 * This is the database layer that will definately see the most use throughout the users of the script.
 * It contains all of the major functions that php has for mysql databases, and should be more than
 * adequate for our needs
 *
 * @author <a href="mailto:gantt@cs.montana.edu">Ryan Gantt</a>
 * @version $Revision: 9 $ $Date: 2010-04-25 05:02:05 +0000 (Sun, 25 Apr 2010) $
 */
class MySQL extends Database implements SQL
{
        /**
         * The current command for MySQL. Changes with every method call
         *
         * @var
         */
        private $queryString;
        protected $linkId;
        private $resultId;

        /**
         * Constructor does nothing but call an existing method to connect.
         *
         * @param host hostname of the database
         * @param user username for the database
         * @param pass password for the database
         * @access public
         */
        public function __construct( Address $host, $user, $pass, $connect=false )
        {
                if(($connect) && ($host instanceof Address))
                {
                        try
                        {
                                $this->connect( $host, $user, $pass );
                        }
                        catch(Error $error)
                        {
                                $error->resolve(__LINE__);
                        }
                }
        }

        /**
         * Initiates the connection to a mysql database.
         *
         * @param host hostname for mysql connection. 'localhost' is fine in most cases
         * @param user username for mysql connection.
         * @param pass password for mysql connection.
         * @return boolean true iff the connection was successful
         * @exception DatabaseException::CouldNotConnect if connection is unsuccessful
         * @access public
         */
        public function connect( Address $host="", $user="", $pass="" )
        {
                $linkId = mysql_connect($host->getServerName(), $user, $pass);
                if($linkId)
                {
                        $this->linkId = $linkId;
                        return $this->linkId;
                }
                else
                {
                        throw new Exception("Database error!: " . mysql_error($this->linkId));
                }
        }

        /**
         * Selects a MySQL database to work with
         *
         * @param dbName the name of the database
         * @return boolean true if the database was successfully selected
         * @exception DatabaseException::SelectDatabase if the database was unsuccessful in being selected
         * @access public
         */
        public function select_db( $dbName )
        {
                if(mysql_select_db($dbName, $this->linkId))
                {
                        return true;
                }
                else
                {
                        throw new Exception(mysql_error($this->linkId));
                }
        }

        /**
         * Run a query command on the database
         *
         * @param query the command query to execute on the database
         * @return queryId if the command was legally and successfully executed on the database
         * @exception DatabaseException::BadQueryString if the query is unescaped or otherwise unacceptable for database use
         * @access public
         */
        public function query( $query )
        {
                $queryId = mysql_query($query, $this->linkId);
                if(!$queryId)
                {
                        throw new Exception("Database Error!: " . mysql_error($this->linkId));
                }
                else
                {
                        $this->resultId = $queryId;
                        return $queryId;
                }
        }

        /**
         * Unset, Unlink, Destroy, Delete (Gets rid of) a MySQL database connection
         *
         * @param linkId the connection to close, defaults to the default connection
         * @access public
         */
        public function close()
        {
                mysql_close($this->linkId);
        }

        public function fetch_object( $query )
        {
                $queryId = mysql_query($query);
                if(!$queryId)
                {
                        throw new Exception(mysql_error($this->linkId));
                }
                else
                {
                        return mysql_fetch_object( mysql_data_seek( $this->linkId, $counter ), $this->linkId );
                }
        }

        /**
         * Count the number of result rows generated by a mysql query command
         *
         * @param query the command to execute that will generate rows (hopefully, if its a legal string)
         * @return int the number of result rows
         * @exception DatabaseException::GetNumRows if for some reason the query string is illegal or we can't access mysql_* functions for some reason
         * @access public
         */
        public function num_rows( $query )
        {
                $rows = mysql_num_rows($query);
                if(!$rows)
                {
                        throw new Exception(mysql_error($this->linkId));
                }
                else
                {
                        return (int)$rows;
                }
        }

        public function fetch_row( $index, $type )
        {
                if($index && $type)
                {
                        mysql_data_seek($this->resultId, $index);
                }
                switch($type)
                {
                        case 'array':
                        default:
                                return mysql_fetch_array($this->resultId());
                }
        }

        /**
         * Deletes all the member variables that were used in this class
         *
         * @access public
         */
        public function __destruct()
        {
                $this->linkId = NULL;
                $this->queryString = NULL;
        }

		public function fetch_array( $query )
		{
			return;
		}

		public function num_fields()
		{
			return;
		}
}
?>
